added some development tools
[windows-sources.git] / developer / Samples / NET 4.6 / Samples for Parallel / Sudoku / Sudoku_VisualBasic / Utilities / GraphicsHelpers.vb
blob9f9ff6ac0d972403b6b72bc70209862451c2f88d
1 '--------------------------------------------------------------------------
2 '
3 ' Copyright (c) Microsoft Corporation. All rights reserved.
4 '
5 ' File: GraphicsHelpers.vb
7 ' Description: Graphics utility class.
8 '
9 '--------------------------------------------------------------------------
11 Imports System.Drawing.Drawing2D
13 Namespace Microsoft.ParallelComputingPlatform.ParallelExtensions.Samples.Sudoku.Utilities
14 ''' <summary>Graphics utility class.</summary>
15 Friend NotInheritable Class GraphicsHelpers
16 ''' <summary>Creates a GraphicsPath that represents a rounded rectangle.</summary>
17 ''' <param name="width">The width of the rectangle.</param>
18 ''' <param name="height">The height of the rectangle.</param>
19 ''' <param name="diameter">The diameter of the rounded corners.</param>
20 ''' <returns>The rounded rectangle path.</returns>
21 Private Sub New()
22 End Sub
23 Public Shared Function CreateRoundedRectangle(ByVal width As Integer, ByVal height As Integer, ByVal diameter As Integer) As GraphicsPath
24 Dim path As New GraphicsPath()
26 Dim upperLeft As New Rectangle(0, 0, diameter, diameter)
27 Dim upperRight As New Rectangle(width - diameter, 0, diameter, diameter)
28 Dim lowerRight As New Rectangle(width - diameter, height - diameter, diameter, diameter)
29 Dim lowerLeft As New Rectangle(0, height - diameter, diameter, diameter)
31 With path
32 .StartFigure()
33 .AddArc(upperLeft, 180, 90)
34 .AddArc(upperRight, 270, 90)
35 .AddArc(lowerRight, 0, 90)
36 .AddArc(lowerLeft, 90, 90)
37 .CloseFigure()
38 End With
40 Return path
41 End Function
43 ''' <summary>Determines the maximum font em size to be used within a particular width and height area.</summary>
44 ''' <param name="text">The text for which we need to know the maximum size.</param>
45 ''' <param name="graphics">The graphics to use to analyze font size.</param>
46 ''' <param name="fontFamily">The FontFamily to size.</param>
47 ''' <param name="fontStyle">The FontStyle to size.</param>
48 ''' <param name="width">The width of the area to contain the drawn character.</param>
49 ''' <param name="height">The height of the area to contain the drawn character.</param>
50 ''' <returns></returns>
51 Public Shared Function GetMaximumEMSize(ByVal text As String, ByVal graphics As Graphics, ByVal fontFamily As FontFamily,
52 ByVal fontStyle As FontStyle, ByVal width As Single, ByVal height As Single) As Single
53 ' Binary search for the best size with at most MAX_ERROR error
54 Const MAX_ERROR = 0.25F
55 Dim curMin = 1.0F, curMax As Single = width
56 Dim emSize = ((curMax - curMin) / 2.0F) + curMin
57 Do While curMax - curMin > MAX_ERROR AndAlso curMin >= 1
58 Using f As New Font(fontFamily, emSize, fontStyle)
59 Dim size = graphics.MeasureString(text, f)
60 Dim textFits = size.Width < width AndAlso size.Height < height
61 If textFits AndAlso emSize > curMin Then
62 curMin = emSize
63 ElseIf (Not textFits) AndAlso emSize < curMax Then
64 curMax = emSize
65 End If
66 End Using
67 emSize = ((curMax - curMin) / 2.0F) + curMin
68 Loop
69 Return curMin ' curMin is the size last known to fit completely, so we use that
70 End Function
71 End Class
72 End Namespace